package com.ddwl.goods.service;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.goods.dao.BannerDao;
import com.ddwl.goods.mapper.BannerMapper;
import com.ddwl.goods.model.Banner;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.BannerBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.BannerVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private BannerDao bannerDao;

    public List<BannerVo> bannerList(Integer platform) {

        //默认查商城平台（2商城 1分销商城）
        if (platform == null) {
            platform = 2;
        }

        List<BannerVo> list = new ArrayList<>();
        for (Banner banner : bannerMapper.bannerList(platform)) {
            BannerVo bannerVo = new BannerVo();
            BeanUtils.copyProperties(banner, bannerVo);
            list.add(bannerVo);
        }
        return list;
    }

    public String create(BannerBo bannerBo) {
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerBo, banner);
        if (bannerBo.getId() != null) {
            bannerDao.updateByPrimaryKeySelective(banner);
        } else {
            if (bannerBo.getPlatform() == null || bannerBo.getPlatform().toString() == "") {
                banner.setPlatform(Constant.PLATFORM_MALL);
            }
            banner.setId(UUIDGenerator.getShortUUID());
            banner.setBannerStatus(BaseEnum.StatusEnum.DISABLE.getCode());
            bannerDao.insert(banner);
        }
        return banner.getId();
    }

    public BannerVo detail(String bannerId) {
        BannerVo bannerVo = new BannerVo();
        Banner banner = bannerDao.selectByPrimaryKey(bannerId);
        BeanUtils.copyProperties(banner, bannerVo);
        return bannerVo;
    }

    public PageVo<BannerVo> conList(PageBo<BannerBo> pageBo) {
//        if(pageBo.getParam().getPlatform()==null||pageBo.getParam().getPlatform().toString()==""){
//            pageBo.getParam().setPlatform(Constant.PLATFORM_MALL);
//        }
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        PageInfo<Banner> pageInfo = new PageInfo<>(bannerMapper.findAll(pageBo.getParam()));
        List<BannerVo> list = BeanConverUtils.convertBeanList(pageInfo.getList(), BannerVo.class);
        return new PageVo<>(pageInfo.getTotal(), pageBo.getPage(), pageBo.getPageSize(), list);
    }


    public void forbidden(BannerBo bannerBo) {
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerBo, banner);
        bannerMapper.updateByPrimaryKey(banner);
    }

    public String delete(String id) {
        bannerDao.deleteById(id);
        return id + Constant.HAVE_DELETED;
    }

}
